﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player2 : MonoBehaviour {
    //[HideInInspector] //Inspector中隐藏变量
    //[SerializeField]  //Inspector中显示变量
    public enum STATE
    {
        IDLE,//静止状态 0
        WALK,//走  1
        RUN,//跑  2
        JUMP,//跳 3
        Attack,//普通攻击 4
        BeiAtt,//被攻击 5
    }
    private Rigidbody2D rigidbody2d;//定义刚体 利用刚体rigidbody2d.velocity线性速度移动
    private Animator _animator;
    //private Transform groundCheck;//判断玩家是否接触地面的标志
    float _timer, bjTime;
    //private float H ; //横向移动
    //private float V ; //纵向移动
    private float speed;//当前的速度
    private bool FacingRight = true;//控制角色转向？？
    private bool OnGround=false; //判断是否在地面
    private bool Jumping = false;//是否在跳跃
    private float JSpeed;//跳的速度
    bool attEd;//被击状态

    public float Movespeed = 3f;//走动速度
    public float Runspeed = 6f; //跑步速度
    public float JumpForce = 300f; //跳跃时增加力，控制添加的rigidbody2d刚体力的大小
    public STATE m_state;
    private void Awake()
    {
        //groundCheck = GameObject.Find("Ground").GetComponent<Transform>().transform;  //查找？？位置？
        rigidbody2d = GetComponent<Rigidbody2D>();//获取刚体组件
        _animator = GetComponent<Animator>();//获取动画控制器
        FacingRight = true;//保证初始状态所面朝的方向为true方向(正方向为true)
        attEd = false;
        bjTime = 0;
    }
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        //OnGround = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));//线投射??????
        //H = Input.GetAxis("Horizontal");//获取水平输入 a键 为 -1  d键为 1
        //V = Input.GetAxis("Vertical");  //获取垂直输入 s键 为 -1  w键为 1

        SpeedAndAnimator();//调用控制动画和速度的函数
    }

    //在Update之前执行  因为主角游戏对象要使用到刚体力，所以一定要写在FixedUpdate里面，不能放在Update上
    private void FixedUpdate()
    {
        AnimatorStateInfo animatorState = _animator.GetCurrentAnimatorStateInfo(0);//获取当前动画器状态信息
                                                                                   //Vector3 screen = Camera.main.WorldToScreenPoint(transform.position); ) //物体的世界坐标转化成屏幕坐标

        Vector3 Position = Camera.main.WorldToScreenPoint(transform.position); //获取坐标
        _timer += Time.deltaTime;//时间
        #region  //人物移动
        Move();
        #endregion

        if (Input.GetKeyDown(KeyCode.X)&& !attEd) //普通攻击
        {
            m_state = STATE.Attack;
        }
    }

    //人物移动
    void Move()
    {
        if ( !attEd) 
        {
            //if (H > 0.3 || H < -0.3)
            //{
            //    m_state = STATE.WALK;
            //    rigidbody2d.velocity = new Vector2(H * speed, 0);  //刚体移动
            //    //transform.Translate(speed * Time.deltaTime, 0, 0);
            //}
            //rigidbody2d.gravityScale = 0f;//刚体受到重力为0
            if (Input.GetKey(KeyCode.RightArrow))  //键盘输入 右方向键
            {
                //Move(STATE.MOVERIGHT);
                m_state = STATE.WALK;
                transform.Translate(speed * Time.deltaTime, 0, 0);
                if (!FacingRight)//转向
                {
                    TrunBack();
                }
            }
            else if (Input.GetKey(KeyCode.LeftArrow))  //键盘输入 左方向键
            {
                m_state = STATE.WALK;
                transform.Translate(-speed * Time.deltaTime, 0, 0);
                if (FacingRight)//转向
                {
                    TrunBack();
                }
            }
            else
            {
                m_state = STATE.IDLE;
            }
        }

        //velocity直接修改物体的速度，无视各种外力
        //addforce直接模仿物理受力了，给物体施加一个力，也会收到其他力的作用
        if (Input.GetKeyDown(KeyCode.Space) && !attEd && !Jumping) 
        {
            OnGround = false;
            Jumping = true;
            m_state = STATE.JUMP;
            rigidbody2d.AddForce(new Vector2(0, JumpForce));
            //rigidbody2d.AddForce(Vector2.up*350,ForceMode2D.Force);
        }
    }

    //控制动画和移动速度
    void SpeedAndAnimator()
    {
        switch(m_state)
        {
            case STATE.WALK:
                if (OnGround)
                {
                    _animator.SetInteger("state", 1);
                }
                speed = Movespeed;
                break;
            case STATE.RUN:
                _animator.SetInteger("state", 2);
                speed = Runspeed;
                break;
            case STATE.IDLE:
                if (OnGround)
                {
                    _animator.SetInteger("state", 0);
                }
                speed = 0;
                break;
            case STATE.JUMP:
                //播放跳跃动画
                _animator.SetInteger("state", 3);
                break;
            case STATE.Attack:
                _animator.SetInteger("state", 4);
                break;
            case STATE.BeiAtt:
                _animator.SetInteger("state", 5);
                bjTime += Time.deltaTime;
                if(bjTime>0.3f)
                {
                    bjTime = 0;
                    attEd = false;
                    m_state = STATE.IDLE;
                }
                break;
        }
    }

    //人物转身
    void TrunBack()
    {
        FacingRight = !FacingRight;
        Vector3 theScale = this.transform.localScale;
        theScale.x *= -1;  //物体镜像翻转
        transform.localScale = theScale;
    }

    private void OnTriggerStay2D(Collider2D collision)
    {
        //忽略物理引擎，没有碰撞效果
        if (collision.gameObject.tag.Equals("EnemAtt"))
        {
            Debug.Log("Duang!!");
            attEd = true;
            bjTime = 0;
            m_state = STATE.BeiAtt;
        }
    }
    //Enter当进入触发器,Exit当退出触发器,Stay当逗留触发器
    private void OnCollisionEnter2D(Collision2D collision)
    {
        //进入碰撞器执行的代码
        if (collision.gameObject.tag.Equals("Ground"))
        {
            Debug.Log("OK");
            OnGround = true;
            Jumping = false;
        }
    }
    //private void OnCollisionExit2D(Collision2D collision)
    //{   //退出碰撞器执行的代码
    //    if(collision.gameObject.tag.Equals("Ground"))
    //    {
    //        Debug.Log("Exit");
    //        OnGround = false;
    //    }
    //}


}
